<?php

namespace Jinxing\Admin\Http\Controllers\Article;

use Illuminate\Http\JsonResponse;
use Jinxing\Admin\Http\Controllers\Controller;
use Jinxing\Admin\Repositories\Article\ArticlesRepository;
use Jinxing\Admin\Repositories\Article\ArticleTypesRepository;
use Jinxing\Admin\Http\Requests\Admin\Article\Articles\StoreRequest;
use Jinxing\Admin\Http\Requests\Admin\Article\Articles\UpdateRequest;
use Jinxing\Admin\Http\Requests\Admin\Article\Articles\DestroyRequest;

class ArticlesController extends Controller
{
    /**
     * @var ArticleTypesRepository
     */
    private $articleTypesRepository;

    public function __construct(ArticlesRepository $articlesRepository, ArticleTypesRepository $articleTypesRepository)
    {
        $this->repository             = $articlesRepository;
        $this->articleTypesRepository = $articleTypesRepository;
    }

    /**
     * 首页显示
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index()
    {
        $types = $this->articleTypesRepository->pluck([
            'status' => 1,
            'order'  => 'sort asc, type_id asc',
        ], 'name', 'type_id');
        return view('admin::article.articles.index', compact('types'));
    }

    public function create()
    {
        view()->share([
            'title'           => trans('添加文章信息'),
            '__active_menu__' => 'admin/article/articles/index',
        ]);

        $types = $this->articleTypesRepository->pluck([
            'status' => 1,
            'order'  => 'sort asc, type_id asc',
        ], 'name', 'type_id');

        return view('admin::article.articles.create', compact('types'));
    }

    /**
     * 添加数据
     *
     * @param StoreRequest $request
     *
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|JsonResponse
     * @throws \Littlebug\Repository\Exception
     */
    public function store(StoreRequest $request)
    {
        // ajax 直接返回
        $result = $this->repository->create($request->all());
        if ($request->ajax()) {
            return $this->success($result);
        }

        return redirect(admin_url('article/articles/index'));
    }

    /**
     * 显示编辑页面
     *
     * @param DestroyRequest $request
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function edit(DestroyRequest $request)
    {
        view()->share([
            'title'           => trans('编辑文章信息'),
            '__active_menu__' => 'admin/article/articles/index',
        ]);
        $types = $this->articleTypesRepository->pluck([
            'status' => 1,
            'order'  => 'sort asc, type_id asc',
        ], 'name', 'type_id');
        $info  = $this->repository->find($request->get('id'));
        return view('admin::article.articles.edit', compact('types', 'info'));
    }

    /**
     * 修改数据
     *
     * @param UpdateRequest $request
     *
     * @return \Illuminate\Contracts\Foundation\Application|JsonResponse|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     * @throws \Littlebug\Repository\Exception
     */
    public function update(UpdateRequest $request)
    {
        // ajax 直接返回
        $result = $this->repository->update($request->input('id'), $request->all());
        if ($request->ajax()) {
            return $this->success($result);
        }

        // 判断结果返回
        return redirect(admin_url('article/articles/index'));
    }

    /**
     * 删除数据
     *
     * @param DestroyRequest $request
     *
     * @return JsonResponse
     * @throws \Littlebug\Repository\Exception
     */
    public function destroy(DestroyRequest $request)
    {
        return $this->success($this->repository->delete($request->input('id')));
    }
}
